package com.xhdx.threadpool;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class InvokeAllTest {
    @SuppressWarnings("rawtypes")
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();  
        
        Set<Callable<String>> callables = new HashSet<Callable<String>>();  
          
        callables.add(new Callable<String>() {  
            public String call() throws Exception {  
                System.out.println("任务 1");
                return "任务 1";  
            }  
        });  
        callables.add(new Callable<String>() {  
            public String call() throws Exception {
                System.out.println("任务 2");
                return "任务 2";  
            }  
        });  
        callables.add(new Callable<String>() {  
            public String call() throws Exception {
                System.out.println("任务 3");
                return "任务 3";  
            }  
        });  
          
        List<Future<String>>  result = null;
        try {
            result = executorService.invokeAll(callables);
            System.out.println("结果:");
            for(Future f :result){  
                System.out.println(f.get());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }  
        executorService.shutdown();
    }
}
